package com.css.fxfzxczh.common.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.css.fxfzxczh.common.rest.model.bo.*;
import com.css.fxfzxczh.common.rest.model.param.*;
import com.css.fxfzxczh.common.rest.model.param.JcDataVersionPageParam;
import com.css.fxfzxczh.common.rest.model.param.JcsjDataViewParam;
import com.css.fxfzxczh.common.rest.model.param.JcsjVersionPageParam;
import com.css.fxfzxczh.modules.buildingKilometerGrid.exceptions.BizException;

import com.css.fxfzxczh.modules.buildingKilometerGrid.model.param.ListByRangeParam;
import com.css.fxfzxczh.modules.buildingKilometerGrid.model.param.SysDataVersionPageParam;

import com.css.fxfzxczh.util.PlatformObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slw.common.helper.PropsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 基础数据api调用
 */
@RestController
@Slf4j
@Component
public class JcsjRestManager {

    @Autowired
    private RestTemplate restTemplate;
    // 查询人口网格版本信息接口列表接口url
    private static final String POPUATION_VERSION_PAGE_URL = "datacenter/sysDataVersion/page";
    // 查询版本信息接口列表接口url
    private static final String VERSION_PAGE_URL = "datacenter/sysDataVersion/dzzhfxpgyqhxt/page";
    // 查询元数据信息接口
    private static final String GET_META_DATA_BY_VERSION_CODE_URL = "datacenter/sysDataVersion/dzzhfxpgyqhxt/getMetaDataByVersionCode";
    // 查询数据视图接口
    private static final String QUERY_DATA_VIEW_URL = "datacenter/sysDataVersion/dzzhfxpgyqhxt/queryDataView";
    // 查询数据视图接口不分页
    private static final String QUERY_DATA_VIEW_LIST_URL = "datacenter/sysDataVersion/dzzhfxpgyqhxt/queryDataViewList";
    // 导出接口
    private static final String DATA_EXPORT_URL = "datacenter/sysDataVersion/dataExport";
    private static final String EXPORT_DATA_VIEW_URL = "datacenter/sysDataVersion/exportDataView";
    // 查询数据类别接口
    private static final String GET_CHILDREN_BY_DICT_CODE = "test/dictItems/getDictItemByDictCode";
    // 根据空间数据查询人口公里网格
    private static final String JC_POPULATION_GRID_GET_BY_GEOM = "datacenter/jcPopulationGrid/dzzhfxpgyqhxt/getByGeom";
    private static final String JC_POPULATION_GRID_GET_BY_PROVINCE = "datacenter/jcPopulationGrid/dzzhfxpgyqhxt/getByProvince";
    private static final String LD = "jcLd/dzzhfxpgyqhxt/listAsPost";
    private static final String LD1 = "jcLd/dzzhfxpgyqhxt/listAsPost1";
    private static final String ListByGeomText = "districtQuery/queryProvinceCityCountyNameCodeListByGeomText";
    private static final String PGA = "jcPga/dzzhfxpgyqhxt/listAsPost";
    private static final String PGA1 = "jcPga/dzzhfxpgyqhxt/listAsPost1";
    private static final String getByProvinceByIds = "datacenter/jcBoua5/getByProvinceByIds";
    private static final String getByCitByIds = "datacenter/jcBoua5/getByCitByIds";
    private static final String getByCountyByName = "datacenter/jcBoua5/getByCountyByName";
    private static final String getGeomByCode = "datacenter/jcBoua5/getGeomByCode";
    private static final String getUnionGeomByCode = "datacenter/jcBoua5/getUnionGeomByCodeList";

    private static final String GEOGRAPHIC_LOCATION_BY_RANGE = "chinaGrid/listByRange";
    private static final String GET_CHINA_GRID_ID_NEW = "chinaGrid/getChinaGridIdNew";

    private static final String GETPGA = "basicLibrary/getByRegion";

    private static final String GET_LIFELINE = "lifelineEngineering/getLifelineDataSource";
    private static final String GET_ANALYSIS_BY_TYPE_AND_ID = "lifelineEngineering/getByTypeAndId";

    /**
     * 获取基础请求路径
     *
     * @return
     */
    private static String getBaseUrl(String apiUrl) throws BizException {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String jcsjRestUrl = configGroup.getProperty("sjxtRestUrl");
        if (StringUtils.isBlank(jcsjRestUrl)) {
            throw new BizException("配置文件未发现基础数据系统接口URL");
        }
        if (!StringUtils.endsWith(jcsjRestUrl, "/")) {
            jcsjRestUrl += "/";
        }
        return jcsjRestUrl + apiUrl;
    }

    /**
     * 获取基础请求路径-承灾体易损性系统
     *
     * @return
     */
    private static String getBaseUrl2(String apiUrl) throws BizException {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String jcsjRestUrl = configGroup.getProperty("ysxRestUrl");
        if (StringUtils.isBlank(jcsjRestUrl)) {
            throw new BizException("配置文件未发现基础数据系统接口URL");
        }
        if (!StringUtils.endsWith(jcsjRestUrl, "/")) {
            jcsjRestUrl += "/";
        }
        return jcsjRestUrl + apiUrl;
    }

    /**
     * 格式化参数为get方式的请求参数字符串
     *
     * @param param
     * @param <T>
     * @return
     */
    private static <T> String buildGetMethodParam(T param) {
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(param, SerializerFeature.WriteDateUseDateFormat));
        StringBuilder str = new StringBuilder("?");
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            str.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }

        String s = str.toString();
        if (s.endsWith("?")) {
            return StringUtils.removeEnd(s, "?");
        } else {
            return StringUtils.removeEnd(s, "&");
        }
    }

    /**
     * 查询版本信息列表接口
     *
     * @param param
     */
    public JcsjVersionPageBo versionPage(JcsjVersionPageParam param) throws BizException {
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(buildBaseHeaders());
        String url = getBaseUrl(VERSION_PAGE_URL) + buildGetMethodParam(param);
        try {
            return restTemplate.exchange(url, HttpMethod.GET, entity, JcsjVersionPageBo.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", param);
            throw e;
        }
    }

    /**
     * 查询元数据信息接口
     *
     * @param versionCode 版本号
     */
    public JcsjVersionMetaInfoBo getMetaDataByVersionCode(String versionCode) throws BizException {
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(buildBaseHeaders());
        String url = getBaseUrl(GET_META_DATA_BY_VERSION_CODE_URL) + "/" + versionCode;
        try {
            return restTemplate.exchange(url, HttpMethod.GET, entity, JcsjVersionMetaInfoBo.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", versionCode);
            throw e;
        }
    }

    /**
     * 查询数据视图接口
     *
     * @param param 查询参数
     * @return
     * @throws BizException
     */
    public JcsjDataViewBo queryDataView(JcsjDataViewParam param) throws BizException {
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(buildBaseHeaders());
        String url = getBaseUrl(QUERY_DATA_VIEW_LIST_URL) + buildGetMethodParam(param);
        try {
            return restTemplate.exchange(url, HttpMethod.GET, entity, JcsjDataViewBo.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", param);
            throw e;
        }
    }

    /**
     * 版本数据导出
     *
     * @param idList
     * @return
     * @throws BizException
     */
    public Resource versionDataExport(List<String> idList) throws BizException {
        HttpEntity<List<String>> entity = new HttpEntity<>(idList, buildBaseHeaders());
        String url = getBaseUrl(DATA_EXPORT_URL);
        try {
            ResponseEntity<Resource> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, Resource.class);
            return exchange.getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", idList);
            throw e;
        }
    }

    /**
     * 数据视图导出
     *
     * @param param 参数
     * @return
     * @throws BizException
     */
    public Resource exportDataView(JcDataVersionPageParam param) throws BizException {
        HttpEntity<JcDataVersionPageParam> entity = new HttpEntity<>(param, buildBaseHeaders());
        String url = getBaseUrl(EXPORT_DATA_VIEW_URL);
        try {
            ResponseEntity<Resource> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, Resource.class);
            return exchange.getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", param);
            throw e;
        }
    }

    /**
     * 查询数据类别接口
     * 根据父级code查询子级信息
     *
     * @param code
     * @return
     */
    public JcsjDataTypeBo getDictItemByDictCode(String code) throws BizException {
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(buildBaseHeaders());
        String url = getBaseUrl(GET_CHILDREN_BY_DICT_CODE) + "/" + code;
        try {
            return restTemplate.exchange(url, HttpMethod.GET, entity, JcsjDataTypeBo.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", code);
            throw e;
        }
    }

    //    @GetMapping("getJcPopulationGridGetByGeom")
    public JcPopulationGridBo getJcPopulationGridGetByGeom(String geom) throws BizException {
        HttpEntity<String> entity = new HttpEntity<>(geom, buildBaseHeaders());
        String url = getBaseUrl(JC_POPULATION_GRID_GET_BY_GEOM);
        try {
            return restTemplate.exchange(url, HttpMethod.POST, entity, JcPopulationGridBo.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", geom);
            throw e;
        }
    }

    public JcPopulationGridByProvinceBo getJcPopulationGridGetByProvince(String province, String city, String county) throws BizException {
        HttpEntity<String> entity = new HttpEntity<>(buildBaseHeaders());
        Map<String, String> param = new HashMap<>();
        param.put("province", province);
        param.put("city", city);
        param.put("county", county);
        String url = getBaseUrl(JC_POPULATION_GRID_GET_BY_PROVINCE) + buildGetMethodParam(param);
        try {
            return restTemplate.exchange(url, HttpMethod.GET, entity, JcPopulationGridByProvinceBo.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", province);
            throw e;
        }
    }

    public JcBoua5getByProvinceByIdsBo getByProvinceByIds(List<String> list) throws BizException {
        HttpEntity<List<String>> entity = new HttpEntity<>(list, buildBaseHeaders());
        String url = getBaseUrl(getByProvinceByIds);
        try {
            return restTemplate.exchange(url, HttpMethod.POST, entity, JcBoua5getByProvinceByIdsBo.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            throw e;
        }
    }

    public JcBoua5getByProvinceByIdsBo getByCitByIds(List<String> list) throws BizException {
        HttpEntity<List<String>> entity = new HttpEntity<>(list, buildBaseHeaders());
        String url = getBaseUrl(getByCitByIds);
        try {
            return restTemplate.exchange(url, HttpMethod.POST, entity, JcBoua5getByProvinceByIdsBo.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            throw e;
        }
    }

    //    @PostMapping("getByCountyByNames")
//    public JcBoua5getByProvinceByIdsBo getByCountyByNames(@RequestBody List<String> list) throws BizException {
    public JcBoua5getByCountyNameBo getByCountyByName(String name) throws BizException {
        HttpEntity<String> entity = new HttpEntity<>(name, buildBaseHeaders());
        String url = getBaseUrl(getByCountyByName);
        try {
            return restTemplate.exchange(url, HttpMethod.POST, entity, JcBoua5getByCountyNameBo.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            throw e;
        }
    }

    public String getGeomByCode(String code) throws BizException {
        HttpEntity<String> entity = new HttpEntity<>(code, buildBaseHeaders());
        String url = getBaseUrl(getGeomByCode);
        try {
            JSONObject body = restTemplate.exchange(url, HttpMethod.POST, entity, JSONObject.class).getBody();
            if (body == null) {
                return null;
            }
            return (String) body.get("data");
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            throw e;
        }
    }

    public String getUnionGeomByCodeList(List<String> codeList) throws BizException {
        HttpEntity<List<String>> entity = new HttpEntity<>(codeList, buildBaseHeaders());
        String url = getBaseUrl(getUnionGeomByCode);
        try {
            JSONObject body = restTemplate.exchange(url, HttpMethod.POST, entity, JSONObject.class).getBody();
            if (body == null) {
                return null;
            }
            return (String) body.get("data");
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            throw e;
        }
    }

    //    @PostMapping("getChinaGridIdNew")
    public GetChinaGridBo getChinaGridIdNew(String geomText, String province, String city, String county) throws BizException {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        JSONObject param = new JSONObject();
        param.put("geomText", geomText);
        param.put("province", province);
        param.put("city", city);
        param.put("county", county);
        HttpEntity<JSONObject> entity = new HttpEntity<>(param, buildBaseHeaders());
        String url = getBaseUrl(GET_CHINA_GRID_ID_NEW);
        try {
            return restTemplate.exchange(url, HttpMethod.POST, entity, GetChinaGridBo.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            throw e;
        } finally {
            stopWatch.stop();
            log.debug("调用基础数据全国格网接口,url：{},用时：{}s,参数:[geomText：{},province：{},city：{},county：{}]", url, stopWatch.getTotalTimeSeconds(), geomText, province, city, county);
        }
    }

    public ChinaGridBo getGeographicLocationByRange(ListByRangeParam param) throws BizException {
        long startTime = System.currentTimeMillis();
        HttpEntity<ListByRangeParam> entity = new HttpEntity<>(param, buildBaseHeaders());
        String url = getBaseUrl(GEOGRAPHIC_LOCATION_BY_RANGE);
        //设置超时时间
        SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        clientHttpRequestFactory.setConnectTimeout(0);
        clientHttpRequestFactory.setReadTimeout(0);

        restTemplate.setRequestFactory(clientHttpRequestFactory);
        try {
            ChinaGridBo body = restTemplate.exchange(url, HttpMethod.POST, entity, ChinaGridBo.class).getBody();
            long endTime = System.currentTimeMillis();
            log.info("远程调用用时:{}", (endTime - startTime) + "ms");
            return body;
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            throw e;
        }
    }

    /**
     * 构建一个基础的请求头
     *
     * @return
     */
    private static HttpHeaders buildBaseHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "dzzhfxpgyqhxt");
        headers.setContentType(MediaType.APPLICATION_JSON);
        return headers;
    }

    //通过省市县获取基础数据的ld的数据
    public JcLDBo getLDList(String province, String city, String county, String version) throws BizException {
        JSONObject param = new JSONObject();
        param.put("province", province);
        param.put("city", city);
        param.put("county", county);
        if (!"".equals(version)) {
            param.put("dataVersion", version);
        }
        HttpEntity<JSONObject> entity = new HttpEntity<>(param, buildBaseHeaders());
        String url = getBaseUrl(LD);
        System.out.println(url);
        RestTemplate restTemplateDgDevice = new RestTemplate();
        SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        clientHttpRequestFactory.setConnectTimeout(30000);
        clientHttpRequestFactory.setReadTimeout(30000);
        restTemplateDgDevice.setRequestFactory(clientHttpRequestFactory);
        try {
            JcLDBo body = restTemplateDgDevice.exchange(url, HttpMethod.POST, entity, JcLDBo.class).getBody();
            return body;
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", province);
            throw e;
        }
    }
    //通过空间数据获取基础数据的ld的数据
    public DistrictEntityBo getPCCByGeomText(String geom) throws BizException {
        JSONObject param = new JSONObject();
        param.put("geomText", geom);
        HttpEntity<JSONObject> entity = new HttpEntity<>(param, buildBaseHeaders());
        String url = getBaseUrl(ListByGeomText);
        System.out.println(url);
        RestTemplate restTemplateDgDevice = new RestTemplate();
        SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        clientHttpRequestFactory.setConnectTimeout(30000);
        clientHttpRequestFactory.setReadTimeout(30000);
        restTemplateDgDevice.setRequestFactory(clientHttpRequestFactory);
        try {
            DistrictEntityBo body = restTemplateDgDevice.exchange(url, HttpMethod.POST, entity, DistrictEntityBo.class).getBody();
            return body;
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            throw e;
        }
    }

    //通过空间数据获取基础数据的ld的数据
    public JcLDBo getLDList1(String geom, String version) throws BizException {
        JSONObject param = new JSONObject();
        param.put("extends10", geom);
        if (PlatformObjectUtils.isNotEmpty(version)) {
            param.put("dataVersion", version);
        }
        HttpEntity<JSONObject> entity = new HttpEntity<>(param, buildBaseHeaders());
        String url = getBaseUrl(LD1);
        System.out.println(url);
        RestTemplate restTemplateDgDevice = new RestTemplate();
        SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        clientHttpRequestFactory.setConnectTimeout(30000);
        clientHttpRequestFactory.setReadTimeout(30000);
        restTemplateDgDevice.setRequestFactory(clientHttpRequestFactory);
        try {
            JcLDBo body = restTemplateDgDevice.exchange(url, HttpMethod.POST, entity, JcLDBo.class).getBody();
            return body;
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            throw e;
        }
    }

    //获取
    public JcPgaBo getPagList(String province, String city, String county, String version) throws BizException {
        JSONObject param = new JSONObject();
        param.put("province", province);
        param.put("city", city);
        param.put("county", county);
        if (!"".equals(version)) {
            param.put("dataVersion", version);
        }
        HttpEntity<JSONObject> entity = new HttpEntity<>(param, buildBaseHeaders());
        String url = getBaseUrl(PGA);
        System.out.println(url);
        //设置超时时间
        RestTemplate restTemplateDgDevice = new RestTemplate();
        SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        clientHttpRequestFactory.setConnectTimeout(30000);
        clientHttpRequestFactory.setReadTimeout(30000);
        restTemplate.setRequestFactory(clientHttpRequestFactory);
        try {
            return restTemplateDgDevice.exchange(url, HttpMethod.POST, entity, JcPgaBo.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", province);
            throw e;
        }
    }

    //通过空间数据获取基础数据的pga的数据
    public JcPgaBo getPagList1(String geom, String version) throws BizException {
        JSONObject param = new JSONObject();
        param.put("extends10", geom);
        if (PlatformObjectUtils.isNotEmpty(version)) {
            param.put("dataVersion", version);
        }
        HttpEntity<JSONObject> entity = new HttpEntity<>(param, buildBaseHeaders());
        String url = getBaseUrl(PGA1);
        System.out.println(url);
        //设置超时时间
        RestTemplate restTemplateDgDevice = new RestTemplate();
        SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        clientHttpRequestFactory.setConnectTimeout(30000);
        clientHttpRequestFactory.setReadTimeout(30000);
        restTemplate.setRequestFactory(clientHttpRequestFactory);
        try {
            return restTemplateDgDevice.exchange(url, HttpMethod.POST, entity, JcPgaBo.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            throw e;
        }
    }

    public JSONObject populationVersionPage(SysDataVersionPageParam param) throws BizException {
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(buildBaseHeaders());
        String url = getBaseUrl(POPUATION_VERSION_PAGE_URL) + buildGetMethodParam(param);
        try {
            return restTemplate.exchange(url, HttpMethod.GET, entity, JSONObject.class).getBody().getJSONObject("data");
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", param);
            throw e;
        }
    }

    //获取
    public JSONArray getPgaOfYsx(String province, String city, String county) throws BizException {
        JSONObject param = new JSONObject();
        param.put("province", province);
        param.put("city", city);
        param.put("county", county);
        HttpEntity<JSONObject> entity = new HttpEntity<>(param, buildBaseHeaders());
        String url = getBaseUrl2(GETPGA);
        System.out.println(url);
        //设置超时时间
        RestTemplate restTemplateDgDevice = new RestTemplate();
        SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        clientHttpRequestFactory.setConnectTimeout(30000);
        clientHttpRequestFactory.setReadTimeout(30000);
        restTemplate.setRequestFactory(clientHttpRequestFactory);
        try {
            return restTemplateDgDevice.exchange(url, HttpMethod.POST, entity, JSONObject.class).getBody().getJSONArray(
                    "data");
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", province);
            throw e;
        }
    }


    //获取
    public JSONObject getBasicLibraryPageForGet(String toUrl,JSONObject param) throws BizException {

        HttpEntity<JSONObject> entity = new HttpEntity<>(buildBaseHeaders());
        String url = getBaseUrl2(toUrl)+ buildGetMethodParam(param);
        System.out.println(url);
        //设置超时时间
        RestTemplate restTemplateDgDevice = new RestTemplate();
        SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        clientHttpRequestFactory.setConnectTimeout(30000);
        clientHttpRequestFactory.setReadTimeout(30000);
        restTemplate.setRequestFactory(clientHttpRequestFactory);
        try {
            return restTemplateDgDevice.exchange(url, HttpMethod.GET, entity, JSONObject.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", param.toString());
            throw e;
        }
    }

    //获取
    public JSONObject getBasicLibraryPageForPost(String toUrl,JSONObject param) throws BizException {

        HttpEntity<JSONObject> entity = new HttpEntity<>(param, buildBaseHeaders());
        String url = getBaseUrl2(toUrl);
        System.out.println(url);
        //设置超时时间
        RestTemplate restTemplateDgDevice = new RestTemplate();
        SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        clientHttpRequestFactory.setConnectTimeout(30000);
        clientHttpRequestFactory.setReadTimeout(30000);
        restTemplate.setRequestFactory(clientHttpRequestFactory);
        try {
            return restTemplateDgDevice.exchange(url, HttpMethod.POST, entity, JSONObject.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", param.toString());
            throw e;
        }
    }

    public YsxLifelineDataBo getLifelineDataSource(YsxDataSourcePageParam param) throws BizException {
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(buildBaseHeaders());
        String url = getBaseUrl2(GET_LIFELINE) + buildGetMethodParam(param);
        try {
            return restTemplate.exchange(url, HttpMethod.GET, entity, YsxLifelineDataBo.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", param);
            throw e;
        }
    }

    /**
     * 生命线查询详情
     *
     * @return
     */
    public Object getAnalysisByTypeAndId(AnalysisParam param) throws BizException {
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(buildBaseHeaders());
        String url = getBaseUrl2(GET_ANALYSIS_BY_TYPE_AND_ID) + buildGetMethodParam(param);
        try {
            return restTemplate.exchange(url, HttpMethod.GET, entity, Object.class).getBody();
        } catch (RestClientException e) {
            log.info("error -> url: {}", url);
            throw e;
        }
    }
}
